package spu

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetSpuDetailByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetSpuDetailByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetSpuDetailByIdApiLogic {
	hostname, _ := os.Hostname()
	return &GetSpuDetailByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetSpuDetailByIdApiLogic) GetSpuDetailByIdApi(req *types.IdReq) (resp *types.SpuModel, err error) {
	var spuBo = bo.SpuBo{}
	if err = dao.SpuEntity.WithContext(l.ctx).Where(dao.SpuEntity.ID.Eq(req.Id)).Select(
		dao.SpuEntity.ALL,
		dao.UnitEntity.Name.As("UnitName"),
		dao.BrandEntity.Name.As("BrandName"),
		dao.SupplierEntity.Name.As("SupplierName"),
		dao.ProductionLineEntity.Name.As("ProductionLineName"),
	).
		LeftJoin(dao.UnitEntity, dao.UnitEntity.ID.EqCol(dao.SpuEntity.UnitID)).
		LeftJoin(dao.BrandEntity, dao.BrandEntity.ID.EqCol(dao.SpuEntity.BrandID)).
		LeftJoin(dao.SupplierEntity, dao.SupplierEntity.ID.EqCol(dao.SpuEntity.SupplierID)).
		LeftJoin(dao.ProductionLineEntity, dao.ProductionLineEntity.ID.EqCol(dao.SpuEntity.ProductionLineID)).
		Scan(&spuBo); err != nil {
		return nil, errors.New("查询失败")
	}
	return &types.SpuModel{
		ID:                 spuBo.ID,                                 // 主键id
		Name:               spuBo.Name,                               // 商品名称
		SpuType:            spuBo.SpuType,                            // 商品类型,0表示基础商品,1表示加工商品
		CategoryId:         spuBo.CategoryId,                         // 商品分类,只保存最后一级的
		CategoryName:       spuBo.CategoryName,                       // 商品名称
		CategoryIDList:     spuBo.CategoryIDList,                     // 分类列表,多个用英文,拼接
		UnitID:             spuBo.UnitID,                             // 商品,关联到unit表主键id
		UnitName:           spuBo.UnitName,                           // 单位名称
		IsWeighed:          spuBo.IsWeighed,                          // 是否称重,0表示是,1表示否
		SalesPrice:         spuBo.SalesPrice.StringFixed(2),          // 销售价
		Sort:               spuBo.Sort,                               // 排序
		Status:             spuBo.Status,                             // 上架状态:1是上架,2是下架
		ProductImageURL:    spuBo.ProductImageURL,                    // 商品图
		SupplierID:         spuBo.SupplierID,                         // 供应商id
		SupplierName:       spuBo.SupplierName,                       // 供应商名称
		BrandID:            spuBo.BrandID,                            // 品牌id
		BrandName:          spuBo.BrandName,                          // 品牌名称
		LossRate:           spuBo.LossRate.StringFixed(2),            // 损耗率
		PurchasePrice:      spuBo.PurchasePrice.StringFixed(2),       // 采购价格
		Content:            spuBo.Content,                            // 商品详情
		CreatedAt:          utils.TimestampToString(spuBo.CreatedAt), // 创建时间
		UpdatedAt:          utils.TimestampToString(spuBo.UpdatedAt), // 更新时间
		ProductionLineID:   spuBo.ProductionLineID,                   // 生产线关联到production_line主键id
		ProductionLineName: spuBo.ProductionLineName,                 // 生产线名称
		Period:             spuBo.Period,                             // 保质期
		IsSelfPurchase:     spuBo.IsSelfPurchase,                     // 是否自备采购
	}, nil
}
